package io.yunho.srw.lean.webflux;

import org.springframework.http.MediaType;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @author: yunho
 * @date: 2021/5/12 14:59
 * @description:
 */
public class SSE extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType(MediaType.TEXT_EVENT_STREAM_VALUE);
        resp.setCharacterEncoding("utf-8");
        for (int i = 0; i < 5; i++) {
            //指定事件标识
            //注意格式
            resp.getWriter().write("event:me\n");
            resp.getWriter().write("data:"+i+"\n\n");
            resp.getWriter().flush();
            try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}
        }
    }
}
